// 新增一个其它物质成本
function addMachinery() {
	var num_of_machinery = $('input[name="machine_name[]"]').length + 1; // 即将添加的其它物质成本编号

	// 添加行的HTML
	var trMachinery = '<tr id="machine-'+num_of_machinery+'">'+
						'<td>'+num_of_machinery+'</td>'+
						'<td><input type="text" name="machine_name[]" id="machine-name-'+num_of_machinery+'" class="input-small validate[required]" autocomplete="off"></td>'+
						'<td>'+
							'<div class="input-append"><input type="text" name="machine_cost_per_unit[]" id="machine-cost-per-unit-'+num_of_machinery+'" class="input-mini validate[required, custom[number], min[0]]" autocomplete="off"><span class="add-on">元</span></div>'+
						'</td>'+
						'<td>'+
							'<div class="input-append"><input type="text" name="machine_number_of_unit[]" id="machine-number-of-unit-'+num_of_machinery+'" class="input-xmini validate[required, custom[integer], min[0]]" autocomplete="off"><span class="add-on">台</span></div>'+
						'</td>'+
						'<td>'+
							'<div class="input-append"><input type="text" name="machine_lifetime[]" id="machine-lifetime-'+num_of_machinery+'" class="input-xmini validate[required, custom[integer], min[0]]" autocomplete="off"><span class="add-on">年</span></div>'+
						'</td>'+
						'<td><select class="autowidth" name="machine_fuel_type[]" id="machine-fuel-type-'+num_of_machinery+'">'+
								'<option value="">请选择</option>'+
								'<option value="柴油">柴油</option>'+
								'<option value="汽油">汽油</option>'+
							'</select>'+
						'</td>'+
					'</tr>';

	// 增加前先验证有无空项
	// if(validateMachineryGroupEmpty() && validateMachineryDup()){
	$("#machinery").append(trMachinery);
	
	// 绑定"燃油"关联问题
	$('[id="machine-fuel-type-'+num_of_machinery+'"]').change(thereIsGasoline);	
	$('[id="machine-fuel-type-'+num_of_machinery+'"]').change(noDiesel);
	// }

}

// 减少一个其它物质成本
function removeMachinery(){
	var num_of_machinery = $('input[name="machine_name[]"]').length + 1; // 即将添加的其它物质成本编号
 	$('#machine-'+(num_of_machinery - 1)).remove();
}

// 验证重复[其它物质名]
function validateMachinery(){
	if($('no-machinery').prop('checked', false))	// 已选择无"机械投入"
		return true;

	var name_inputs = $('input[name="machine_name[]"]');

	var total_num = name_inputs.length;

	var strAry = [];
	var dup_index = 0;
	for (var i = 0; i < total_num; i++) {
		if(name_inputs[i].value != '')
			strAry[i] = name_inputs[i].value;
	};

	for (var i = 0; i < strAry.length; i++) {
		for (var j = i + 1; j < strAry.length; j++)
			if(strAry[i] == strAry[j]) {
				dup_index = j; break;
			}
	};

	if(dup_index != 0) {
		// alert("first duplicate index: "+dup_index+", total_num "+total_num);
		$(name_inputs[dup_index]).validationEngine('showPrompt', "* 已有重复的项目存在", "error", true);
		return false;
	}
	return true;
}

// 验证当前其它物质信息是否填写完全
// function validateMachineryGroupEmpty(){

// 	var name_inputs = $('input[name="machine_name[]"]');

// 	var total_num = name_inputs.length;

// 	for(var i = 1; i <= total_num; i++) {
// 		name_is_valid = !($('[id="machine-name-' + i + '"]').validationEngine('validate')); // 验证单项失败返回true
// 		cost_is_valid = !($('[id="machine-cost-per-unit-' + i + '"]').validationEngine('validate'));
// 		num_is_valid = !($('[id="machine-number-of-unit-' + i + '"]').validationEngine('validate'));
// 		lifetime_is_valid = !($('[id="machine-lifetime-' + i + '"]').validationEngine('validate'));
// 		// fuel_is_valid = !($('[id="machine-fuel-type-' + i + '"]').validationEngine('validate'));
// 		if ((name_is_valid && cost_is_valid && num_is_valid && lifetime_is_valid) == false) {
// 			// 有未填项
// 			return false; // 有空缺项返回false
// 		};
// 	}
// 	return true; // 填写完整返回true
// }

// 动态显示汽油油价问题
function thereIsGasoline(){
	// if($(this).val() == '汽油') {
	// 	$('#local-gasoline-price-div').show();
	// 	return true;
	// }
	var fuel_type_inputs = $('select[name="machine_fuel_type[]"]');
	var total_num = fuel_type_inputs.length;
	var there_is_gasoline = 0;
	for(var i = 0; i < total_num; i++) {
		if(fuel_type_inputs[i].value == '汽油' && $("#no-cost-fuel").prop("checked") == false) {
			there_is_gasoline = 1;
		}
	}

	if(there_is_gasoline == 1) {
		$('#local-gasoline-price-div').show();
		return true;
	}
	else {
		$('#local-gasoline-price-div').hide();
		return false;
	}
}

function noDiesel(){
	var fuel_type_inputs = $('select[name="machine_fuel_type[]"]');
	var total_num = fuel_type_inputs.length;
	var no_diesel_flag = 1;
	for(var i = 0; i < total_num; i++) {
		if(fuel_type_inputs[i].value == '柴油' && $("#no-cost-fuel").prop("checked") == false) {
			no_diesel_flag = 0;
		}
	}

	if(no_diesel_flag == 1) {
		$('#local-diesel-price-div').hide();
		return true;
	}
	else {
		$('#local-diesel-price-div').show();
		return false;
	}
}